﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HiLand.Utility.IO;
using HiLand.Utility4.Office;

namespace OpenXMLDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string filePath = @"C:\Users\然\Desktop\OpenXMLDemo\test.docx";

            WordHelper.ReplaceTextContent(filePath, "/word/document.xml", "海澜网络", "紫光海澜网络技术");

            // word/media/image2.png
            Stream newIamgeStream = new FileStream(@"C:\Users\然\Desktop\OpenXMLDemo\productName.png", FileMode.Open, FileAccess.Read);
            Stream docxStream = new FileStream(@"C:\Users\然\Desktop\OpenXMLDemo\test.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            WordHelper.ReplaceSteamContent(docxStream, string.Format("/word/media/image2.png"), newIamgeStream);
        }

        //private static void ReplaceSteamContent(Stream docxStream, string innerfileFullName, Stream newContentStream)
        //{
        //    try
        //    {
        //        //打开docx文件  
        //        using (Package zip = Package.Open(docxStream, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        //        {
        //            foreach (PackagePart part in zip.GetParts())
        //            {
        //                string imageFileFullPath =  innerfileFullName;

        //                //寻找内部xml文件  
        //                if (part.Uri.OriginalString == imageFileFullPath)
        //                {
        //                    string contentType = part.ContentType;
        //                    CompressionOption option = part.CompressionOption;
        //                    PackageRelationshipCollection relations = part.GetRelationships();

        //                    //删除xml文档
        //                    zip.DeletePart(part.Uri);

        //                    //创建一个新的xml文档
        //                    Uri tempUri = PackUriHelper.CreatePartUri(new Uri(imageFileFullPath, UriKind.Relative));
        //                    PackagePart tempPart = zip.CreatePart(tempUri, contentType, option);

        //                    //将修改后的xml文档保存
        //                    using (Stream stream = tempPart.GetStream())
        //                    {
        //                        //stream = newImageStream;
        //                        IOHelper.BlocklyCircularlyOperateStream(newContentStream, BlocklyWriteToInnerStream, stream);
        //                        stream.Flush();
        //                        stream.Close();
        //                    }

        //                    //创建tempart与各xml文件之间的关系,非常重要  
        //                    foreach (PackageRelationship relation in relations)
        //                    {
        //                        tempPart.CreateRelationship(relation.TargetUri, relation.TargetMode, relation.RelationshipType, relation.Id);
        //                    }

        //                    zip.Close();
        //                    break;
        //                }
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        //throw new Exception(ex.Message);
        //    }
        //}

        //private static bool BlocklyWriteToInnerStream(object sender, OperateStreamEnventArgs args)
        //{
        //    Stream innerStream = (Stream)args.CallBackObject;
        //    innerStream.Write(args.BytesReaded, 0, args.BytesReaded.Length);
        //    innerStream.Flush();
        //    return true;
        //}

        //private static void ReplaceTextContent(string docxFileFullPath, string innerfileFullName, string originalContent, string newContent)
        //{
        //    try
        //    {
        //        //打开docx文件  
        //        using (Package zip = Package.Open(docxFileFullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        //        {
        //            foreach (PackagePart part in zip.GetParts())
        //            {
        //                //寻找内部xml文件  
        //                if (part.Uri.OriginalString == innerfileFullName)
        //                {
        //                    string content = null;
        //                    using (StreamReader sr = new StreamReader(part.GetStream()))
        //                    {
        //                        content = sr.ReadToEnd();
        //                        //替换内容
        //                        if (content != null)
        //                        {
        //                            content = content.Replace(originalContent, newContent);
        //                        }
        //                        sr.Close();
        //                    }

        //                    string contentType = part.ContentType;
        //                    CompressionOption option = part.CompressionOption;
        //                    PackageRelationshipCollection relations = part.GetRelationships();

        //                    //删除xml文档
        //                    zip.DeletePart(part.Uri);

        //                    //创建一个新的xml文档
        //                    Uri tempUri = PackUriHelper.CreatePartUri(new Uri(innerfileFullName, UriKind.Relative));
        //                    PackagePart tempPart = zip.CreatePart(tempUri, contentType, option);

        //                    //将修改后的xml文档保存
        //                    using (StreamWriter sw = new StreamWriter(tempPart.GetStream()))
        //                    {
        //                        sw.Write(content);
        //                        sw.Flush();
        //                        sw.Close();
        //                    }

        //                    //创建tempart与各xml文件之间的关系,非常重要  
        //                    foreach (PackageRelationship relation in relations)
        //                    {
        //                        tempPart.CreateRelationship(relation.TargetUri, relation.TargetMode, relation.RelationshipType, relation.Id);
        //                    }

        //                    zip.Close();
        //                    break;
        //                }
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        //throw new Exception(ex.Message);
        //    }
        //}
    }
}
